/* Copyright (c) [2025] [zhi1208@126.com]
 * SPDX-License-Identifier: MIT
 */

/*********************
 *      INCLUDES
 *********************/
#include "lvgl.h"
#include "hmi_pages.h"
#include "hmi_config.h"
#include "hmi_top_bar.h"
#include "hmi_anim.h"
#include "hmi_bg.h"
#include "hmi_tinyttf.h"
#include "hmi_pg_navigation.h"
#include "hmi_hal.h"
#include "hmi_loading_toast.h"
#include "hmi_subjects.h"

/*********************
 *      DEFINES
 *********************/

/**********************
 *      TYPEDEFS
 **********************/

/**********************
 *  STATIC PROTOTYPES
 **********************/
static void create();
static void del();
static void enter_anim(lv_anim_completed_cb_t cb, uint32_t delay);
static uint32_t exit_anim(lv_anim_completed_cb_t cb, hmi_pg_t *enter_pg);

static void wifi_connect_status_observer_cb(lv_observer_t *observer, lv_subject_t *subject);

static void cancel_btn_click_cb(lv_event_t *e);
static void confirm_btn_click_cb(lv_event_t *e);

/**********************
 *  STATIC VARIABLES
 **********************/
static hmi_pg_t wifi_pwd = {
    .name = "",
    .del_on_pop = 1,
    .bg_color = 0x7dace4,
    .create = create,
    .del = del,
    .enter_anim = enter_anim,
    .exit_anim = exit_anim,
};

static lv_font_t *def_font_20;

/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
 **********************/
hmi_pg_t *hmi_pg_wifi_pwd()
{
    return &wifi_pwd;
}

/**********************
 *   STATIC FUNCTIONS
 **********************/
static void create()
{
    wifi_pwd.bg_y = HMI_BG_FULL;

    wifi_pwd.root = lv_obj_create(NULL);
    lv_obj_remove_style_all(wifi_pwd.root);
    lv_obj_set_flex_flow(wifi_pwd.root, LV_FLEX_FLOW_COLUMN);
    lv_obj_set_flex_align(wifi_pwd.root, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_SPACE_EVENLY);
    lv_obj_remove_flag(wifi_pwd.root, LV_OBJ_FLAG_SCROLLABLE);

    hmi_top_bar_create(wifi_pwd.root, wifi_pwd.name);

    if (!def_font_20)
        def_font_20 = hmi_tiny_ttf_create_static("def_font", 20, false);

    lv_obj_t *pwd_textarea = lv_textarea_create(wifi_pwd.root);
    lv_obj_set_width(pwd_textarea, lv_pct(94));
    lv_obj_set_style_text_font(pwd_textarea, def_font_20, 0);
    lv_obj_set_style_border_width(pwd_textarea, 0, 0);
    lv_textarea_set_one_line(pwd_textarea, true);
    lv_textarea_set_placeholder_text(pwd_textarea, "请输入密码");

    lv_obj_t *cancel_btn = lv_button_create(wifi_pwd.root);
    lv_obj_add_flag(cancel_btn, LV_OBJ_FLAG_IGNORE_LAYOUT);
    lv_obj_set_size(cancel_btn, lv_pct(30), LV_SIZE_CONTENT);
    lv_obj_set_style_radius(cancel_btn, LV_RADIUS_CIRCLE, 0);
    lv_obj_set_style_bg_color(cancel_btn, lv_color_white(), 0);
    lv_obj_align_to(cancel_btn, pwd_textarea, LV_ALIGN_OUT_BOTTOM_LEFT, 30, 30);
    lv_obj_add_event_cb(cancel_btn, cancel_btn_click_cb, LV_EVENT_CLICKED, NULL);

    lv_obj_t *cancel_label = lv_label_create(cancel_btn);
    lv_obj_set_style_text_font(cancel_label, def_font_20, 0);
    lv_obj_set_style_text_color(cancel_label, lv_color_black(), 0);
    lv_label_set_text(cancel_label, "取消");
    lv_obj_center(cancel_label);

    lv_obj_t *confirm_btn = lv_button_create(wifi_pwd.root);
    lv_obj_add_flag(confirm_btn, LV_OBJ_FLAG_IGNORE_LAYOUT);
    lv_obj_set_size(confirm_btn, lv_pct(30), LV_SIZE_CONTENT);
    lv_obj_set_style_radius(confirm_btn, LV_RADIUS_CIRCLE, 0);
    lv_obj_set_style_bg_color(confirm_btn, lv_color_white(), 0);
    lv_obj_align_to(confirm_btn, pwd_textarea, LV_ALIGN_OUT_BOTTOM_RIGHT, -30, 30);
    lv_obj_add_event_cb(confirm_btn, confirm_btn_click_cb, LV_EVENT_CLICKED, pwd_textarea);

    lv_obj_t *confirm_label = lv_label_create(confirm_btn);
    lv_obj_set_style_text_font(confirm_label, def_font_20, 0);
    lv_obj_set_style_text_color(confirm_label, lv_color_black(), 0);
    lv_label_set_text(confirm_label, "连接");
    lv_obj_center(confirm_label);

    lv_obj_t *kb = lv_keyboard_create(wifi_pwd.root);
    lv_obj_add_flag(kb, LV_OBJ_FLAG_IGNORE_LAYOUT);
    lv_obj_set_style_text_font(kb, &lv_font_montserrat_20, 0);
    lv_obj_set_size(kb, lv_pct(100), lv_pct(45));
    lv_obj_align(kb, LV_ALIGN_BOTTOM_MID, 0, 0);
    lv_keyboard_set_textarea(kb, pwd_textarea);

    lv_subject_add_observer_obj(&hmi_wifi_connect_subj, wifi_connect_status_observer_cb, wifi_pwd.root, NULL);
}
static void del()
{
    lv_obj_delete_async(wifi_pwd.root);
    wifi_pwd.root = NULL;

    if (def_font_20)
    {
        hmi_tiny_ttf_del(def_font_20);
        def_font_20 = NULL;
    }
}
static void enter_anim(lv_anim_completed_cb_t cb, uint32_t delay)
{
    hmi_enter_anim_default(wifi_pwd.root, cb, delay);
}
static uint32_t exit_anim(lv_anim_completed_cb_t cb, hmi_pg_t *enter_pg)
{
    return hmi_exit_anim_default(wifi_pwd.root, cb, enter_pg);
}

static void wifi_connect_status_observer_cb(lv_observer_t *observer, lv_subject_t *subject)
{
    LV_UNUSED(observer);
    LV_UNUSED(subject);

    if (hmi_get_cur_page() != &wifi_pwd)
        return;

    int32_t wifi_state = lv_subject_get_int(&hmi_wifi_subj);

    if (wifi_state)
    {
        hmi_loading_toast_close();
        hmi_pg_push(hmi_pg_ok());
    }
    else
    {
        hmi_loading_toast_close();
        hmi_pg_push(hmi_pg_fail());
    }
}

static void cancel_btn_click_cb(lv_event_t *e)
{
    LV_UNUSED(e);
    hmi_loading_toast_close();
    hmi_pg_pop();
}

static void confirm_btn_click_cb(lv_event_t *e)
{
#ifdef LV_PORT_VS_CODE
    LV_UNUSED(e);
    hmi_pg_push(hmi_pg_fail());
#else
    lv_obj_t *pwd_textarea = lv_event_get_user_data(e);
    hmi_loading_toast_open();
    hmi_hal_wifi_connect(wifi_pwd.name, lv_textarea_get_text(pwd_textarea));
#endif
}